package com.example.tsproject.Controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.Result;
import com.example.tsproject.entity.Students;
import com.example.tsproject.entity.Teachers;
import com.example.tsproject.service.StudentsService;
import com.example.tsproject.service.TeachersService;
import jakarta.mail.Session;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: t-s-project
 * @BelongsPackage: com.example.tsproject.Controller
 * @Author: YoungDra
 * @CreateTime: 2024/10/17  20:54
 */
@RestController

public class LoginByVerifycodeController {
    @Autowired
    private StudentsService studentsService;
    @Autowired
    private TeachersService teachersService;
    @PostMapping("/loginbyemail1")
    public Result loginbyemail1(@RequestParam("email") String email, @RequestParam("verifycode") String verifycode, HttpServletRequest request){

        HttpSession session = request.getSession();
//        System.out.println(session.getAttribute("validcode"));
        String get_verifycode= (String) session.getAttribute("validcode");
//        System.out.println(email);
//        System.out.println(verifycode);
        Teachers teachers = teachersService.findByEmail(email);
        System.out.println(teachers);
        String token="0";
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 3600);
        Map<String,Object> Claims = new HashMap<>();
        Claims.put("id",teachers.getId());
        Claims.put("employee_number",teachers.getEmployee_number());
        Claims.put("name",teachers.getName());
        Claims.put("email",teachers.getEmail());
        Claims.put("avatar",teachers.getAvatar());
        Claims.put("status",teachers.getStatus());
        Claims.put("password",teachers.getPassword());
        token = JWT.create()
                .withClaim("Claims",Claims)  // Payload"
                .withExpiresAt(calendar.getTime())
                .sign(Algorithm.HMAC256("YoungDra"));  // 签名用的secret
        if(get_verifycode==null||!get_verifycode.equals(verifycode))
            return Result.error("验证码错误");
        return Result.success(token);
    }
    @PostMapping("/loginbyemail2")
    public Result loginbyemail2(@RequestParam("email") String email, @RequestParam("verifycode") String verifycode,HttpServletRequest request){

        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("validcode"));
        String get_verifycode= (String) session.getAttribute("validcode");
        System.out.println(email);
        System.out.println(verifycode);
        Students students = studentsService.findByEmail(email);
        if(students==null)
            return Result.error("无该账号信息");
        if(students.getStatus().equals("inactive"))
            return Result.error("账号已被禁用");
        System.out.println(students);
        String token="0";Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 3600);
        Map<String,Object> Claims = new HashMap<>();
        Claims.put("id",students.getId());
        Claims.put("student_number",students.getStudent_number());
        Claims.put("stuclass",students.getStuclass());
        Claims.put("name",students.getName());
        Claims.put("email",students.getEmail());
        Claims.put("avatar",students.getAvatar());
        Claims.put("status",students.getStatus());
        Claims.put("password",students.getPassword());
        token = JWT.create()
                .withClaim("Claims",Claims)  // Payload"
                .withExpiresAt(calendar.getTime())
                .sign(Algorithm.HMAC256("YoungDra"));  // 签名用的secret
        if(get_verifycode==null||!get_verifycode.equals(verifycode))
            return Result.error("验证码错误");
        return Result.success(token);
    }
    @RequestMapping("getinfo")
    public String getinfo(){
        System.out.println("2"+ThreadLocalUtil.get());
        return "0";
    }
}
